/*
 * @copyright: Huang Ding
 * @Author: ding-cx
 * @Date: 2021-02-26 16:57:25
 * @LastEditors: ding-cx
 * @LastEditTime: 2021-03-27 10:53:06
 * @Description: 系统配置表
 */
import { Application } from "egg";
import { DataTypes, Model } from "sequelize";
import { modelCommonField } from "../../utils";

export interface sysConfigInterface extends Model {
  /** 主键id */
  id: number;
  /** 名称 */
  name: string;
  /** 英文键名 */
  key: string;
  /** 内容，可json parse的字符串 */
  value: string;
  /** 备注 */
  memo: string;

  /** 创建者id */
  create_user_id: number;

  /** 修改者id */
  edit_user_id: number;
  /** 创建时间 */
  createdAt: string;
  /** 修改时间 */
  updatedAt: string;
  /** 删除时间 */
  deletedAt: string;
}

export default (app: Application) => {
  const model = app.model.define<sysConfigInterface>(
    app.config.dbModel_prefix + "sys_config",
    {
      name: {
        field: "name",
        comment: "名称",
        type: DataTypes.STRING(40),
      },
      key: {
        field: "key",
        comment: "英文键名",
        type: DataTypes.STRING(80),
        unique: true,
      },
      value: {
        field: "value",
        comment: "英文键名",
        type: DataTypes.STRING(5000),
      },
      memo: {
        field: "memo",
        comment: "备注",
        type: DataTypes.STRING(300),
      },

      ...modelCommonField,
    }
  );

  model.findOrCreate({
    defaults: {
      id: 1,
      key: "oss",
      name: "oss",
      value: JSON.stringify({
        local: { url: "http://localhost:7001/oss/local/file/", dir: "D:/uploads" },
      }),
    }, 
    where: {
      key: "oss",
    },
  });

  return model;
};
